import { loadSceneOrLibraryFromBlob, MIME_TYPES } from "@excalidraw/excalidraw";
import { RestoredDataState } from "@excalidraw/excalidraw/types/data/restore";
import { ImportedLibraryData } from "@excalidraw/excalidraw/types/data/types";

interface ImportedLibraryDatas extends ImportedLibraryData {
    /** @deprecated v1 */
    merge?: boolean;
}
export interface ApiType {
  updateScene: (arg0: RestoredDataState) => void;
  updateLibrary: (arg0: ImportedLibraryDatas) => void;
}

export const addLibs = async (excalidrawAPI: ApiType, file: Blob | File) => {
  const contents = await loadSceneOrLibraryFromBlob(file, null, null);
  console.log('contents',contents)
  if (contents.type === MIME_TYPES.excalidraw) {
    excalidrawAPI.updateScene(contents.data);
  } else if (contents.type === MIME_TYPES.excalidrawlib) {
    excalidrawAPI.updateLibrary({...contents.data, merge: true});
  }
};

export const stringToUTF8ArrayBuffer = (str: string) => {
  var encoder = new TextEncoder();
  return encoder.encode(str).buffer;
};
